package com.example.hyg_veintype.view.veinTypeImgParamsDialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.base.baseadapter.BaseHolder;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;
import com.example.hyg_veintype.data.local.chart.ChartBean;
import com.example.hyg_veintype.utils.DoubleUtils;
import com.example.hyg_veintype.utils.chartUtil.OptimalChartUtils;
import com.example.pulseanalyse.veinPoint.myClassPluseAnalysisSD;
import com.example.pulseanalyse.veinType.MainWindow;
import com.example.pulseanalyse.veinType.myHelper;
import com.github.mikephil.charting.charts.LineChart;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author: yyw
 * time: 2020/12/18 14:34
 * desc: VeinTypeImgParamsDialog脉图参数对话框中VP适配器
 */
public class VeinTypeImgParamsVpAdapter extends BaseRecyclerAdapter<ChartBean> {

    public VeinTypeImgParamsVpAdapter(List<ChartBean> infos, Context context) {
        super(infos, context);
    }

    @Override
    public BaseHolder<ChartBean> getHolder(View v, int viewType) {
        return new MyHolder(v, context);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.veintype_item_vp_veintype_img_params;
    }

    class MyHolder extends BaseHolder<ChartBean> {

        TextView tvPositionName;//采脉位置
        LineChart chartOptimal;//最佳脉图
        LineChart chartAll;//包含最佳脉图的脉图
        TextView tvPressure;//对应“浮”“中”“沉”
        TextView tvPulseRate;//脉率
        TextView tvPulsePower;//脉力
        TextView tvFluency;//流利度
        TextView tvTension;//紧张度
        TextView tvMaiJieLv;//脉节率
        TextView tvJunYunDu;//均匀度
        TextView tvH1;//主波幅高
        TextView tvH3;//潮波幅高
        TextView tvH4;//降中峡h4
        TextView tvH5;//重搏波与降中峡之差h5
        TextView tvT;
        TextView tvT1;
        TextView tvT4;
        TextView tvT5;
        TextView tvW;
        TextView tvα;
        TextView tvθ;
        TextView tvH3H1;
        TextView tvH4H1;
        TextView tvH5H1;
        TextView tvWT;

        public MyHolder(View itemView, Context context) {
            super(itemView, context);
            tvPositionName = itemView.findViewById(R.id.tv_position_name);
            chartOptimal = itemView.findViewById(R.id.chart_optimal);
            chartAll = itemView.findViewById(R.id.chart_all);
            tvPressure = itemView.findViewById(R.id.tv_pressure);
            tvPulseRate = itemView.findViewById(R.id.tv_pulse_rate);
            tvPulsePower = itemView.findViewById(R.id.tv_pulse_power);
            tvFluency = itemView.findViewById(R.id.tv_fluency);
            tvTension = itemView.findViewById(R.id.tv_tension);
            tvMaiJieLv = itemView.findViewById(R.id.tv_maijielv);
            tvJunYunDu = itemView.findViewById(R.id.tv_junyundu);
            tvH1 = itemView.findViewById(R.id.tv_h1);
            tvH3 = itemView.findViewById(R.id.tv_h3);
            tvH4 = itemView.findViewById(R.id.tv_h4);
            tvH5 = itemView.findViewById(R.id.tv_h5);
            tvT = itemView.findViewById(R.id.tv_t);
            tvT1 = itemView.findViewById(R.id.tv_t1);
            tvT4 = itemView.findViewById(R.id.tv_t4);
            tvT5 = itemView.findViewById(R.id.tv_t5);
            tvW = itemView.findViewById(R.id.tv_w);
            tvα = itemView.findViewById(R.id.tv_α);
            tvθ = itemView.findViewById(R.id.tv_θ);
            tvH3H1 = itemView.findViewById(R.id.tv_h3h1);
            tvH4H1 = itemView.findViewById(R.id.tv_h4h1);
            tvH5H1 = itemView.findViewById(R.id.tv_h5h1);
            tvWT = itemView.findViewById(R.id.tv_wt);
        }

        @Override
        public void setData(ChartBean data, int position) {
            tvPositionName.setText(data.getName());
            /************** 2个脉图的数据处理及显示 **************/
            {
                //主波幅高最大的为最佳脉图
                List<Double> sortList = new ArrayList();
                sortList.add(data.getFuSd().p主波幅高);
                sortList.add(data.getZhongSd().p主波幅高);
                sortList.add(data.getChenSd().p主波幅高);
                int index = sortList.indexOf(Collections.max(sortList));
                List<Integer> OptimalList = new ArrayList<>();//最佳脉图的数据
                List<Integer> allList = new ArrayList<>();//包含最佳脉图的数据
                if (index == 0) {
                    allList.addAll(data.getFuSd().listCopy);
                    OptimalList.addAll(data.getFuSd().listCopy.subList(data.getFuSd().myAutoStartIndex, data.getFuSd().myAutoEndIndex + 1));
                    setTextUI(0, data.getFuSd(), data.getWindow());
                } else if (index == 1) {
                    allList.addAll(data.getZhongSd().listCopy);
                    OptimalList.addAll(data.getZhongSd().listCopy.subList(data.getZhongSd().myAutoStartIndex, data.getZhongSd().myAutoEndIndex + 1));
                    setTextUI(1, data.getZhongSd(), data.getWindow());
                } else if (index == 2) {
                    allList.addAll(data.getChenSd().listCopy);
                    OptimalList.addAll(data.getChenSd().listCopy.subList(data.getChenSd().myAutoStartIndex, data.getChenSd().myAutoEndIndex + 1));
                    setTextUI(2, data.getChenSd(), data.getWindow());
                }
                //缩放脉点，使其在图表内均匀分布，最佳脉图
                OptimalChartUtils.setOptimalChart(OptimalList, chartOptimal);
                //缩放脉点，使其在图表内均匀分布，包含最佳脉图的数据
                OptimalChartUtils.setallChart(OptimalList.size(), allList, chartAll);

            }
            /************** 2个脉图的数据处理及end **************/
        }

        /**
         * 根据最佳脉图的脉点分析结果填充数据
         *
         * @param index            0,浮 1,中 2,沉
         * @param _myPluseAyalysis 脉点分析类
         * @param mainWindow       脉型分析类
         */
        private void setTextUI(int index, myClassPluseAnalysisSD _myPluseAyalysis, MainWindow mainWindow) {
            if (index == 0) {
                tvPressure.setText("浮");
                //流利度
                setPulseFluency(mainWindow.PulseTypeFu);
            } else if (index == 1) {
                tvPressure.setText("中");
                //流利度
                setPulseFluency(mainWindow.PulseTypeZhong);
            } else if (index == 2) {
                tvPressure.setText("沉");
                //流利度
                setPulseFluency(mainWindow.PulseTypeChen);
            }
            tvPulseRate.setText(mainWindow.AvePulseRate + "");//脉率
            //脉搏力度
            if (_myPluseAyalysis.p主波幅高 >= 22) {
                tvPulsePower.setText("有力");
            } else if ((_myPluseAyalysis.p主波幅高 > myHelper.StaticH1 * 2 / 3) && (_myPluseAyalysis.p主波幅高 < 21)) {
                tvPulsePower.setText("适中");
            } else {
                tvPulsePower.setText("无力");
            }

            //紧张度
            if (_myPluseAyalysis.p主波上部周期 / _myPluseAyalysis.p周期 > 0.2) {
                tvTension.setText("紧张");
            } else {
                tvTension.setText("正常");
            }
            //脉节律
            if (_myPluseAyalysis.p周期方差 > 0.1) {
                tvMaiJieLv.setText("不均匀");
            } else {
                tvMaiJieLv.setText("均匀");
            }
            //脉力均匀度
            if (_myPluseAyalysis.p脉力方差 > 1) {
                tvJunYunDu.setText("不均匀");
            } else {
                tvJunYunDu.setText("均匀");
            }

            tvH1.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p主波幅高));//主波幅高
            tvH3.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p潮波幅高));//潮波幅高
            tvH4.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.降中峡h4));//降中峡h4
            tvH5.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.重搏波与降中峡之差h5));//重搏波与降中峡之差h5
            tvT.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p周期));
            tvT1.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p主波上升期));
            tvT4.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.t4));
            tvT5.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.t5));
            tvW.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p主波上部周期));
            tvα.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p主波入射角));
            tvθ.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.p主波夹角));
            tvH3H1.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.h3h1));
            tvH4H1.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.h4h1));
            tvH5H1.setText(DoubleUtils.double2ToStr(_myPluseAyalysis.h5h1));
            BigDecimal b1 = new BigDecimal(Double.toString(_myPluseAyalysis.p主波上部周期));
            BigDecimal b2 = new BigDecimal(Double.toString(_myPluseAyalysis.p主波上升期));
            String WT = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
            tvWT.setText(WT);
        }

        /**
         * 流利度
         *
         * @param pluseType 脉型
         */
        public void setPulseFluency(String pluseType) {
            if (pluseType.contains("滑")) {
                tvFluency.setText("流利");
            } else if (pluseType.contains("涩")) {
                tvFluency.setText("滞涩");
            } else {
                tvFluency.setText("正常");
            }
        }

    }
}
